package com.library.book.service.impl;

import com.library.book.dao.BorrowingBooksMapper;
import com.library.book.dao.DepartmentMapper;
import com.library.book.dao.UserMapper;
import com.library.book.dao.BookMapper;
import com.library.book.po.*;
import com.library.book.service.UserService;
import com.library.book.utils.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

    @Service
    public class UserServiceImpl implements UserService {

        @Resource
        private UserMapper userMapper;
        @Resource
        private DepartmentMapper departmentMapper;
        @Resource
        private BorrowingBooksMapper borrowingBooksMapper;
        @Resource
        private BookMapper bookMapper;

        @Override
        public List<User> findUserByUserName(String userName) {

            UserExample userExample = new UserExample();
            UserExample.Criteria criteria = userExample.createCriteria();

            criteria.andUserNameEqualTo(userName);
            List<User> users = userMapper.selectByExample(userExample);

            return users;
        }


        @Override
        public List<Department> findAllDepts() {
            return departmentMapper.selectByExample(new DepartmentExample());
        }

        @Override
        public User userLogin(String userName, String password) {

            List<User> users = findUserByUserName(userName);

            if (null == users) {
                return null;
            }

            for (User user : users) {
                if (user.getUserPwd().equals(password)) {
                    return user;
                }
            }
            return null;
        }

        @Override
        public boolean updateUser(User user, HttpServletRequest request) {

            User sessionUser = (User) request.getSession().getAttribute("user");
            user.setUserId(sessionUser.getUserId());

            int n = userMapper.updateByPrimaryKey(user);

            if (n > 0) {

                User newUser = userMapper.selectByPrimaryKey(user.getUserId());
                request.getSession().setAttribute("user", newUser);
                return true;
            }

            return false;
        }


        public List<BorrowingBooksPo> findAllBorrowingBooks(HttpServletRequest request) {
            User user = (User) request.getSession().getAttribute("user");

            BorrowingBooksExample borrowingBooksExample = new BorrowingBooksExample();
            BorrowingBooksExample.Criteria criteria = borrowingBooksExample.createCriteria();
            criteria.andUserIdEqualTo(user.getUserId());
            List<BorrowingBooks> borrowingBooksList = borrowingBooksMapper.selectByExample(borrowingBooksExample);
            if (null == borrowingBooksList) {
                return null;
            }

            List<BorrowingBooksPo> res = new LinkedList<BorrowingBooksPo>();
            for (BorrowingBooks borrowingBooks : borrowingBooksList) {
                Book book = bookMapper.selectByPrimaryKey(borrowingBooks.getBookId());
                BorrowingBooksPo borrowingBooksPo = new BorrowingBooksPo();
                borrowingBooksPo.setBook(book);

                Date date1 = borrowingBooks.getDate();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                String dateOfBorrowing = sdf.format(date1);

                Calendar calendar = Calendar.getInstance();
                calendar.setTime(date1);
                calendar.add(Calendar.MONTH, 2);//增加两个月
                Date date2 = calendar.getTime();
                String dateOfReturn = sdf.format(date2);

                borrowingBooksPo.setDateOfBorrowing(dateOfBorrowing);
                borrowingBooksPo.setDateOfReturn(dateOfReturn);
                res.add(borrowingBooksPo);
            }
            return res;
        }

        @Override
        public boolean userReturnBook(int bookId, HttpServletRequest request) {
            User user = (User) request.getSession().getAttribute("user");
            BorrowingBooksExample borrowingBooksExample = new BorrowingBooksExample();
            BorrowingBooksExample.Criteria criteria = borrowingBooksExample.createCriteria();
            criteria.andUserIdEqualTo(user.getUserId());
            criteria.andBookIdEqualTo(bookId);

            int n = borrowingBooksMapper.deleteByExample(borrowingBooksExample);
            if (n > 0) return true;
            return false;
        }

        @Override
        public boolean userBorrowingBook(int bookId, HttpServletRequest request) {
            User user = (User) request.getSession().getAttribute("user");

            BorrowingBooksExample borrowingBooksExample = new BorrowingBooksExample();
            BorrowingBooksExample.Criteria criteria = borrowingBooksExample.createCriteria();
            criteria.andBookIdEqualTo(bookId);
            List<BorrowingBooks> list = borrowingBooksMapper.selectByExample(borrowingBooksExample);

            if (list.size() > 0) {
                return false;
            }

            BorrowingBooks borrowingBooks = new BorrowingBooks();
            borrowingBooks.setBookId(bookId);
            borrowingBooks.setUserId(user.getUserId());
            borrowingBooks.setDate(new Date());
            int n = 0;


            try {
                n = borrowingBooksMapper.insert(borrowingBooks);
            } catch (Exception e) {
                return false;
            }


            if (n > 0) {
                return true;
            }
            return false;
        }

        @Override
        public User findUserById(int id) {
            return userMapper.selectByPrimaryKey(id);
        }

        @Override
        public Page<User> findUserByPage(int pageNum) {
            List<User> users = userMapper.selectByPageNum((pageNum - 1) * 10, 10);
            Page<User> page = new Page<>();
            page.setList(users);
            page.setPageNum(pageNum);
            page.setPageSize(10);

            int userCount = userMapper.selectUserCount();
            int pageCount = userCount / 10;
            if (userCount % 10 != 0) {
                pageCount++;
            }
            page.setPageCount(pageCount);
            return page;
        }

        @Override
        public int insertUser(User user) {
            return userMapper.insert(user);
        }

        @Override
        public int deleteUserById(int userId) {
            return userMapper.deleteByPrimaryKey(userId);
        }
    }
